/******************************************************************************
 *  Copyright (C) SNDA CORPORATION
 *
 *  ALL RIGHTS RESERVED BY SNDA CORPORATION,  THIS PROGRAM
 *  MUST BE USED SOLELY FOR THE PURPOSE FOR WHICH IT WAS
 *  FURNISHED BY SNDA CORPORATION,  NO PART OF THIS PROGRAM
 *  MAY BE REPRODUCED OR DISCLOSED TO OTHERS,  IN ANY FORM
 *  WITHOUT THE PRIOR WRITTEN PERMISSION OF SNDA CORPORATION.
 *
 *  SNDA CONFIDENTIAL AND PROPRIETARY
 *
 ******************************************************************************
 *  !!! 由MessageCompiler自动生成的代码，请不要手工修改内容。
 *  生成时间: Wed Sep 05 15:37:32 CST 2012
 *****************************************************************************/
#import "msgbuf.h"

const static Byte MB_NULL = -1;
const static Byte MB_NOT_NULL = 0;
const static Byte MB_TRUE = -1;
const static Byte MB_FALSE = 0;
const static NSUInteger BUFFER_TRUNC_SIZE = 1024;

///////////////////////////////////////////////////////////////////////////////
// Implementation of NSData (NSDataExtension)
// Source from URL: // http://www.cocoadev.com/index.pl?NSDataCategory
///////////////////////////////////////////////////////////////////////////////
@implementation NSData (NSDataExtension)

- (NSData *)zlibInflate {
	if ([self length] == 0) {
        return self;
    }
    
	unsigned full_length = (unsigned) [self length];
	unsigned half_length = (unsigned) [self length] / 2;
    
	NSMutableData *decompressed = [[NSMutableData alloc] initWithLength: full_length + half_length];
	BOOL done = NO;
	int status;
    
	z_stream strm;
	strm.next_in = (Bytef *)[self bytes];
	strm.avail_in = (unsigned) [self length];
	strm.total_out = 0;
	strm.zalloc = Z_NULL;
	strm.zfree = Z_NULL;
    
	if (inflateInit (&strm) != Z_OK) {
        [decompressed release];
        return nil;
    }
    
	while (!done) {
		// Make sure we have enough room and reset the lengths.
		if (strm.total_out >= [decompressed length]) {
			[decompressed increaseLengthBy: half_length];
        }
        
		strm.next_out = [decompressed mutableBytes] + strm.total_out;
		strm.avail_out = (unsigned) ([decompressed length] - strm.total_out);
        
		// Inflate another chunk.
		status = inflate (&strm, Z_SYNC_FLUSH);
		if (status == Z_STREAM_END) {
            done = YES;
		} else if (status != Z_OK) {
            break;
        }
	}
    
	if (inflateEnd (&strm) != Z_OK) {
        [decompressed release];
        return nil;
    }
    
	// Set real length.
    NSData *retval = nil;
	if (done) {
		[decompressed setLength: strm.total_out];
		retval = [[NSData alloc] initWithData: decompressed];
	}
    
    [decompressed release];
	return [retval autorelease];
}

- (NSData *)zlibDeflate {
	if ([self length] == 0) {
        return self;
    }
	
	z_stream strm;
    
	strm.zalloc = Z_NULL;
	strm.zfree = Z_NULL;
	strm.opaque = Z_NULL;
	strm.total_out = 0;
	strm.next_in=(Bytef *)[self bytes];
	strm.avail_in = (unsigned) [self length];
    
	// Compresssion Levels:
	//   Z_NO_COMPRESSION
	//   Z_BEST_SPEED
	//   Z_BEST_COMPRESSION
	//   Z_DEFAULT_COMPRESSION
    
	if (deflateInit(&strm, Z_DEFAULT_COMPRESSION) != Z_OK) { 
        return nil;
    }
    
    // 4K chuncks for expansion
	NSMutableData *compressed = [[NSMutableData alloc] initWithLength:4196];
    
	do {
		if (strm.total_out >= [compressed length]) {
			[compressed increaseLengthBy: 4196];
        }
		
		strm.next_out = [compressed mutableBytes] + strm.total_out;
		strm.avail_out = (unsigned) ([compressed length] - strm.total_out);
		
		deflate(&strm, Z_FINISH);  
		
	} while (strm.avail_out == 0);
	
	deflateEnd(&strm);
	
	[compressed setLength: strm.total_out];
	NSData *retval = [[NSData alloc] initWithData: compressed];
    [compressed release];
    return [retval autorelease];
}

- (NSData *)gzipInflate {
	if ([self length] == 0) {
        return self;
    }
	
	unsigned full_length = (unsigned) [self length];
	unsigned half_length = (unsigned) ([self length] / 2);
	
	NSMutableData *decompressed = [[NSMutableData alloc] initWithLength: full_length + half_length];
	BOOL done = NO;
	int status;
	
	z_stream strm;
	strm.next_in = (Bytef *)[self bytes];
	strm.avail_in = (unsigned) [self length];
	strm.total_out = 0;
	strm.zalloc = Z_NULL;
	strm.zfree = Z_NULL;
	
	if (inflateInit2(&strm, (15+32)) != Z_OK) {
        [decompressed release];
        return nil;
    }
    
	while (!done) {
		// Make sure we have enough room and reset the lengths.
		if (strm.total_out >= [decompressed length]) {
			[decompressed increaseLengthBy: half_length];
        }
        
		strm.next_out = [decompressed mutableBytes] + strm.total_out;
		strm.avail_out = (unsigned) ([decompressed length] - strm.total_out);
		
		// Inflate another chunk.
		status = inflate (&strm, Z_SYNC_FLUSH);
		if (status == Z_STREAM_END) {
            done = YES;
		} else if (status != Z_OK) {
            break;
        }
	}
    
	if (inflateEnd (&strm) != Z_OK) {
        [decompressed release];
        return nil;
    }
	
	// Set real length.
    NSData *retval = nil;
	if (done) {
		[decompressed setLength: strm.total_out];
		retval = [[NSData alloc] initWithData: decompressed];
	}
    
    [decompressed release];
	return [retval autorelease];
}

- (NSData *)gzipDeflate {
	if ([self length] == 0) {
        return self;
    }
	
	z_stream strm;
	
	strm.zalloc = Z_NULL;
	strm.zfree = Z_NULL;
	strm.opaque = Z_NULL;
	strm.total_out = 0;
	strm.next_in=(Bytef *)[self bytes];
	strm.avail_in = (unsigned) [self length];
	
	// Compresssion Levels:
	//   Z_NO_COMPRESSION
	//   Z_BEST_SPEED
	//   Z_BEST_COMPRESSION
	//   Z_DEFAULT_COMPRESSION
	
	if (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY) != Z_OK) {
        return nil;
    }
	
    // 4K chunks for expansion
	NSMutableData *compressed = [[NSMutableData alloc] initWithLength:4196];  
	
	do {
		if (strm.total_out >= [compressed length]) {
			[compressed increaseLengthBy: 4196];
		}
        
		strm.next_out = [compressed mutableBytes] + strm.total_out;
		strm.avail_out = (unsigned) ([compressed length] - strm.total_out);
		
		deflate(&strm, Z_FINISH);  
		
	} while (strm.avail_out == 0);
	
	deflateEnd(&strm);
	
	[compressed setLength: strm.total_out];
	NSData *retval = [[NSData alloc] initWithData:compressed];
    [compressed release];
    return [retval autorelease];
}

@end

///////////////////////////////////////////////////////////////////////////////
// Implementation of IOBuffer
///////////////////////////////////////////////////////////////////////////////
@implementation IOBuffer

@synthesize dataBuffer;
@synthesize limit;
@synthesize current;

- (IOBuffer *) init {
    return [self initWithCapacity:BUFFER_TRUNC_SIZE];
}

- (IOBuffer *) initWithCapacity:(NSUInteger)capacity {
    self = [super init];
    if (self) {
        dataBuffer = [[NSMutableData alloc] initWithCapacity:capacity];
        limit = capacity;
        current = 0;
    }
    
    return self;
}

- (void) dealloc {
#ifdef DEBUG
    NSLog(@"**** IOBuffer dealloc");
#endif
    [dataBuffer release];
	[super dealloc];
}

- (void) rewind {
	current = 0;
}

- (void) flip {
    limit = current;
    current = 0;
}

- (void) clear {
    current = 0;
    limit = [dataBuffer length];
//    [dataBuffer resetBytesInRange:(NSRange) {0, limit}];
}

- (NSUInteger) inputRemaining {
	return limit - current;
}

- (NSUInteger) outputRemaining {
    return [dataBuffer length] - current;
}

- (void) increaseCurrent:(NSUInteger) length {
    current += length;
}

- (NSUInteger) copyData:(void *) dest {
    return [self copyData:dest :(NSRange) {0, limit}];
}

- (NSUInteger) copyData:(void *) dest :(NSRange) range {
    [dataBuffer getBytes:dest range:range];
    return range.length - range.location;
}

+ (void) reverseBytesOrder:(void *)bytes :(int)size {
    Byte *p = (Byte *) bytes;
    int half = size / 2;
    Byte v;
    for (int i = 0, j = size - 1; i < half; ++i, --j) {
        v = p[i];
        p[i] = p[j];
        p[j] = v;
    }
}

+ (short) n2lShort:(short)v {
#if BYTE_ORDER == LITTLE_ENDIAN
    [self reverseBytesOrder:&v :sizeof(short)];
#endif
    return v;
}

+ (int) n2lInt:(int)v {
#if BYTE_ORDER == LITTLE_ENDIAN
    [self reverseBytesOrder:&v :sizeof(int)];
#endif
    return v;
}

+ (long long) n2lLong:(long long)v {
#if BYTE_ORDER == LITTLE_ENDIAN
    [self reverseBytesOrder:&v :sizeof(long long)];
#endif
    return v;
}

+ (float) n2lFloat:(float)v {
#if BYTE_ORDER == LITTLE_ENDIAN
    [self reverseBytesOrder:&v :sizeof(float)];
#endif
    return v;
}

+ (double) n2lDouble:(double)v {
#if BYTE_ORDER == LITTLE_ENDIAN
    [self reverseBytesOrder:&v :sizeof(double)];
#endif
    return v;
}

+ (short) l2nShort:(short)v {
#if BYTE_ORDER == LITTLE_ENDIAN
    [self reverseBytesOrder:&v :sizeof(short)];
#endif
    return v;
}

+ (int) l2nInt:(int)v {
#if BYTE_ORDER == LITTLE_ENDIAN
    [self reverseBytesOrder:&v :sizeof(int)];
#endif
    return v;
}

+ (long long) l2nLong:(long long)v {
#if BYTE_ORDER == LITTLE_ENDIAN
    [self reverseBytesOrder:&v :sizeof(long long)];
#endif
    return v;
}

+ (float) l2nFloat:(float)v {
#if BYTE_ORDER == LITTLE_ENDIAN
    [self reverseBytesOrder:&v :sizeof(float)];
#endif
    return v;
}

+ (double) l2nDouble:(double)v {
#if BYTE_ORDER == LITTLE_ENDIAN
    [self reverseBytesOrder:&v :sizeof(double)];
#endif
    return v;
}

- (void) getBytes: (void*) dest: (NSUInteger) length {
    if ([self inputRemaining] < length) {
        @throw [MessageBuffer createException:@"IOBuffer"
                                             :nil
                                             :__FILE__
                                             :__LINE__
                                             :"buffer overflow"];
    } else {
        [self->dataBuffer getBytes:dest range:(NSRange) {current, length}];
        [self increaseCurrent:length];
    }
}

- (Byte) readByte {
    Byte v;
    [self getBytes:&v :sizeof(Byte)];
    return v;
}

- (BOOL) readBoolean {
    Byte v;
    [self getBytes:&v :sizeof(Byte)];
    return v == MB_TRUE ? TRUE : FALSE;
}

- (short) readShort {
    short v = 0;
    [self getBytes:&v :sizeof(short)];
    return [IOBuffer n2lShort:v];
}

- (int) readInt {
    int v = 0;
    [self getBytes:&v :sizeof(int)];
    return [IOBuffer n2lInt:v];
}

- (long long) readLong {
    long long v = 0;
    [self getBytes:&v :sizeof(long long)];
    return [IOBuffer n2lLong:v];
}

- (float) readFloat {
    float v = 0.00;
    [self getBytes:(&v) :sizeof(float)];
    return [IOBuffer n2lFloat:v];
}

- (double) readDouble {
    double v = 0.00;
    [self getBytes:&v :sizeof(double)];
    return [IOBuffer n2lDouble:v];
}

- (NSString*) readUTF {
    NSUInteger len = [self readShort];
    char cstr[len];
    [self getBytes:cstr :len];
    return [[[NSString alloc] initWithBytes:cstr length:len encoding:NSUTF8StringEncoding] autorelease];
}

- (void) extendBufferToContain:(NSUInteger) length {
    NSInteger gap = [self outputRemaining] - length;
    if (gap < 0) {
        NSInteger num = (- gap) / BUFFER_TRUNC_SIZE + 1;
        [self->dataBuffer increaseLengthBy:num * BUFFER_TRUNC_SIZE];
    }
}

- (void) replaceBytes:(const void *)src :(NSRange)range {
    [self->dataBuffer replaceBytesInRange:range withBytes:src];
}

- (void) putBytes:(const void *)src :(NSUInteger)length {
    [self extendBufferToContain:length];
    NSRange range = (NSRange) {current, length};
    [self->dataBuffer replaceBytesInRange:range withBytes:src];
    [self increaseCurrent:length];
}

- (void) putData:(NSData *) data {
    [self putBytes:[data bytes] :[data length]];        
}

- (void) writeByte:(Byte)v {
    [self putBytes:&v :sizeof(Byte)];
}

- (void) writeBoolean:(BOOL)v {
    Byte byte = v ? MB_TRUE : MB_FALSE;
    [self putBytes:&byte :sizeof(Byte)];
}

- (void) writeShort:(short)v {
    v = [IOBuffer l2nShort:v];
    [self putBytes:&v :sizeof(short)];
}

- (void) writeInt:(int)v {
    v = [IOBuffer l2nInt:v];
    [self putBytes:&v :sizeof(int)];
}

- (void) writeLong:(long long)v {
    v = [IOBuffer l2nLong:v];
    [self putBytes:&v :sizeof(long long)];
}

- (void) writeFloat:(float)v {
    v = [IOBuffer l2nFloat:v];
    [self putBytes:&v :sizeof(float)];
}

- (void) writeDouble:(double)v {
    v = [IOBuffer l2nDouble:v];
    [self putBytes:&v :sizeof(double)];
}

- (void) writeUTF:(NSString *)v {
    short len = [v lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    short nlen = [IOBuffer l2nShort:len];
    [self putBytes:&nlen :sizeof(short)];
    [self putBytes:(void*) [v cStringUsingEncoding:NSUTF8StringEncoding] :len];
}

@end

///////////////////////////////////////////////////////////////////////////////
// Implementation of MessageBean
///////////////////////////////////////////////////////////////////////////////
@implementation MessageBean

@synthesize commandId;

- (MessageBean *) init {
    self = [super init];
    if (self) {
    	[self clear];
    }
    
    return self;
}


- (MessageBean *) initWithCommandId:(short)cmdId {
	self = [super init];
	if (self) {
		commandId = cmdId;
	}
	
	return self;
}

- (void) clear {
	commandId = 0;
}

- (void) printToString:(NSMutableString *)string {
	[self printToStringWithIndent:string :0];
}

- (void) appendIndent:(NSMutableString *)string :(int)indentLevel {
	for (int i = 0; i < indentLevel; ++i) {
		[string appendString:@"    "];
	}
}

- (void) printToStringWithIndent:(NSMutableString *)string :(int)indentLevel {
}

- (void) seriallize:(IOBuffer *)obuffer {
    @throw [MessageBuffer createException:@"MessageBean"
                                         :nil
                                         :__FILE__
                                         :__LINE__
                                         :"Un-implemented instance seriallize method."];
}

- (void) deseriallize:(IOBuffer *)ibuffer {
    @throw [MessageBuffer createException:@"MessageBean"
                                         :nil
                                         :__FILE__
                                         :__LINE__
                                         :"Un-implemented instance deseriallize method."];
}

+ (MessageBean *) deseriallize:(IOBuffer *)ibuffer {
    @throw [MessageBuffer createException:@"MessageBean"
	                                     :nil
                                         :__FILE__
                                         :__LINE__
                                         :"Un-implemented class deseriallize method."];
}

@end

///////////////////////////////////////////////////////////////////////////////
// Implementation of MessageBuffer
///////////////////////////////////////////////////////////////////////////////
@implementation MessageBuffer

+ (id) getBeanClassByCommandId:(short)commandId {
	switch (commandId) {
		case -9999:	return nil;
		case -1000:	return nil;
		case 1000:	return nil;
		case 1001:	return [Backpack class];
		case 1002:	return [SceneMap class];
		case 2000:	return [AllType class];
		case 8888:	return nil;
		case 9999:	return nil;
		default:
            @throw [MessageBuffer createException:@"MessageBufer"
                                                 :nil
                                                 :__FILE__
                                                 :__LINE__
                                                 :"Un-defined command ID %d.", (int) commandId];
	}
}

+ (BOOL) isZippedCommand:(short) commandId {
	switch (commandId) {
		case 2000:
			return TRUE;
			
		default:
			return FALSE;
	}
}

+ (NSException *) createException:(NSString *)className :(NSException *) cause: (const char *)file :(int)lineNum :(const char *)reasonFormat, ... {
    va_list argList;
    va_start(argList, reasonFormat);
    
    char tmp[256];
    memset(&tmp, 0, sizeof(tmp));
    vsprintf(tmp, reasonFormat, argList);
    
    NSNumber *ln = [NSNumber numberWithInt:lineNum];
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"File:", [NSString stringWithUTF8String:file],
                          @"Line:", [ln stringValue],
                          @"Cause:", (cause == nil ? @"N/A" : cause),
                          @"Stack:", (cause == nil ? @"N/A" : [cause callStackSymbols]),
                          nil];
    
    return [NSException exceptionWithName:className
                                   reason:[NSString stringWithUTF8String:tmp]
                                 userInfo:dict];
    
}

+ (NSUInteger) pack:(short)commandId :(MessageBean *)bean :(IOBuffer *)obuffer {	
    // 写入一个字节的“是否压缩标识"
    BOOL isZipped = [self isZippedCommand:commandId];
    Byte zipFlag = isZipped ? MB_TRUE : MB_FALSE;
    [obuffer writeByte:zipFlag];
    
    id beanClass = [MessageBuffer getBeanClassByCommandId:commandId];
    if (beanClass == nil) { // 空指令
        return 0;
    } else if (isZipped) {
        IOBuffer *unzippedBuffer = [[IOBuffer alloc] init];
        [MessageBuffer packBean:unzippedBuffer :bean];
        [unzippedBuffer flip];
        Byte *unzippedBytes = (Byte *) [unzippedBuffer.dataBuffer bytes];
        NSUInteger unzippedLength = [unzippedBuffer limit];
        NSData *unzippedData = [[NSData alloc] initWithBytesNoCopy:unzippedBytes
                                                            length:unzippedLength
                                                      freeWhenDone:FALSE];
        NSData *zippedData = [unzippedData gzipDeflate];
        [obuffer putData:zippedData];
        
        //[zippedData release];
        [unzippedData release];
        [unzippedBuffer release];
    } else {
        [MessageBuffer packBean:obuffer : bean];
    }
	
	return obuffer.current;
}

+ (MessageBean *) unpack:(short) commandId :(IOBuffer *) ibuffer {
	@try {
		id beanClass = [MessageBuffer getBeanClassByCommandId:commandId];
        MessageBean *bean = nil;
        
        // 读出“是否压缩标识"
		Byte isZipped = [ibuffer readByte];
        
        if (beanClass == nil) { // 空指令
            bean = [[NullMessageBean alloc] init];
        } else if (isZipped == MB_TRUE) {
            NSUInteger remaining = [ibuffer inputRemaining];
            NSUInteger current = [ibuffer current];
            Byte *zippedBytes = ((Byte *) [ibuffer.dataBuffer bytes]) + current;
            
            NSData *zippedData = [[NSData alloc] initWithBytesNoCopy:zippedBytes
                                                              length:remaining
                                                        freeWhenDone:FALSE];
            NSData *unzippedData = [zippedData gzipInflate];
            IOBuffer *unzippedBuffer = [[IOBuffer alloc] init];
            [unzippedBuffer putData:unzippedData];
            [unzippedBuffer flip];
            bean = [MessageBuffer unpackBean:unzippedBuffer :beanClass];
            
            [unzippedBuffer release];
            //[unzippedData release];
            [zippedData release];
        } else {
            bean = [MessageBuffer unpackBean:ibuffer :beanClass];
        }
        
        bean.commandId = commandId;
        return bean;
	} @catch (NSException *e) {
		NSLog(@"Exception: %@ %@ %@", [e name], [e reason], [e userInfo]);
		return nil;
	}
}

+ (void) packBean:(IOBuffer *) obuffer :(MessageBean *) bean {
    [MessageBuffer writeIsNull:obuffer :bean];
    if (bean == nil) {
        return;
    } else {
        [bean seriallize:obuffer];
    }
}

+ (void) packBeanList:(IOBuffer *)obuffer :(NSMutableArray *)beanList {
    [MessageBuffer writeIsNull:obuffer :beanList];
    if (beanList == nil) {
        return;
    }
    
    long len = [beanList count];
    [obuffer writeShort:len];
    for (int i = 0; i < len; ++i) {
        [MessageBuffer packBean:obuffer: [beanList objectAtIndex:i]];
    }
}

+ (MessageBean *) unpackBean:(IOBuffer *) ibuffer :(id) beanClass {
    if ([MessageBuffer readIsNull:ibuffer]) {
        return nil;
    } else {
        MessageBean *bean = [[beanClass alloc] init];
        [bean deseriallize:ibuffer];
        return [bean autorelease];
    }
}

+ (NSMutableArray *) unpackBeanList:(IOBuffer *) ibuffer :(id) beanClass {
    if ([MessageBuffer readIsNull:ibuffer]) {
        return nil;
    }
    
    short len = [ibuffer readShort];
    NSMutableArray *beanList = [[NSMutableArray alloc] initWithCapacity:len];
    MessageBean *bean = nil;
    for (int i = 0; i < len; ++i) {
    	bean = [MessageBuffer unpackBean:ibuffer :beanClass];
        [beanList addObject:bean];
        //[bean release];
    }
    
    return [beanList autorelease];
}


+ (NSMutableArray *) readArray:(IOBuffer *)ibuffer :(int)dataType {
    if ([MessageBuffer readIsNull:ibuffer]) {
        return nil;
    }
    
    int len = [ibuffer readShort];
    if (len < 0) {
        @throw [MessageBuffer createException:@"MessageBean"
                                             :nil
                                             :__FILE__
                                             :__LINE__
                                             :"Elements number of array is negative."];
    }
    
    NSMutableArray *ary = [[NSMutableArray alloc] initWithCapacity:len];
    NSString *string;
    
    Byte byteValue;
    BOOL boolValue;
    short shortValue;
    int intValue;
    long long longValue;
    float floatValue;
    double doubleValue;
    
    for (int i = 0; i < len; ++i) {
        switch (dataType) {
            case DT_BYTE:
                byteValue = [ibuffer readByte];
                [ary addObject:[NSNumber numberWithChar:byteValue]];
                break;
                
            case DT_BOOL:
                boolValue = [ibuffer readBoolean];
                [ary addObject:[NSNumber numberWithBool:boolValue]];
                break;
                
            case DT_SHORT:
                shortValue = [ibuffer readShort];
                [ary addObject:[NSNumber numberWithShort:shortValue]];
                break;
                
            case DT_INT:
                intValue = [ibuffer readInt];
                [ary addObject:[NSNumber numberWithInt:intValue]];
                break;
                
            case DT_LONG:
                longValue = [ibuffer readLong];
                [ary addObject:[NSNumber numberWithLongLong:longValue]];
                break;
                
            case DT_FLOAT:
                floatValue = [ibuffer readFloat];
                [ary addObject:[NSNumber numberWithFloat:floatValue]];
                break;
                 
            case DT_DOUBLE:
                doubleValue = [ibuffer readDouble];
                [ary addObject:[NSNumber numberWithDouble:doubleValue]];
                break;
                
            case DT_STRING:
                string = [MessageBuffer readString:ibuffer];
                [ary addObject:string];
                //[string release];
                break;
                
            default:
                @throw [MessageBuffer createException:@"MessageBean"
                                                     :nil
                                                     :__FILE__
                                                     :__LINE__
                                                     :"Unkonwn data type: %d.", dataType];
        }
    }
    
    return [ary autorelease];
}

+ (void) writeArray:(IOBuffer *)obuffer :(NSMutableArray *)ary :(int)dataType {
    [self writeIsNull:obuffer :ary];
    if (ary == nil) {
        return;
    }
    
    short len = [ary count];
    [obuffer writeShort:len];
    if (len == 0) {
        return;
    }
    
    NSNumber *number;
    NSString *string;
    NSObject *obj;
    
    for (int i = 0; i < len; ++i) {
        obj = [ary objectAtIndex:i];
        if (IS_NUMBER_TYPE(dataType)) {
            number = (NSNumber *) obj;
        } else if (IS_STRING_TYPE(dataType)) {
            string = (NSString *) obj;
        }
        
        switch (dataType) {
            case DT_BYTE:
                [obuffer writeByte:[number charValue]];
                break;
                
            case DT_BOOL:
                [obuffer writeBoolean:[number boolValue]];
                break;
                
            case DT_SHORT:
                [obuffer writeShort:[number shortValue]];
                break;
                
            case DT_INT:
                [obuffer writeInt:[number intValue]];
                break;
                
            case DT_LONG:
                [obuffer writeLong:[number longLongValue]];
                break;
                
            case DT_FLOAT:
                [obuffer writeFloat:[number floatValue]];
                break;
                
            case DT_DOUBLE:
                [obuffer writeDouble:[number doubleValue]];
                break;
                
            case DT_STRING:
                [MessageBuffer writeString:obuffer :string];
                break;
                
            default:
                @throw [MessageBuffer createException:@"MessageBean"
                                                     :nil
                                                     :__FILE__
                                                     :__LINE__
                                                     :"Unkonwn data type: %d.", dataType];
        }
    }
}

+ (Byte) readByte:(IOBuffer *)ibuffer {
    return [ibuffer readByte];
}

+ (NSMutableArray *) readByteArray:(IOBuffer *)ibuffer {
    return [MessageBuffer readArray:ibuffer :DT_BYTE];
}

+ (BOOL) readBoolean:(IOBuffer *)ibuffer {
    return [ibuffer readByte] == MB_TRUE ? TRUE : FALSE;
}

+ (NSMutableArray *) readBooleanArray:(IOBuffer *)ibuffer {
    return [MessageBuffer readArray:ibuffer :DT_BOOL];
}

+ (short) readShort:(IOBuffer *)ibuffer {
    return [ibuffer readShort];
}

+ (NSMutableArray *) readShortArray:(IOBuffer *)ibuffer {
    return [MessageBuffer readArray:ibuffer :DT_SHORT];}

+ (int) readInteger:(IOBuffer *)ibuffer {
    return [ibuffer readInt];
}

+ (NSMutableArray *) readIntegerArray:(IOBuffer *)ibuffer {
    return [MessageBuffer readArray:ibuffer :DT_INT];

}

+ (long long) readLong:(IOBuffer *)ibuffer {
    return [ibuffer readLong];
}

+ (NSMutableArray *) readLongArray:(IOBuffer *)ibuffer {
    return [MessageBuffer readArray:ibuffer :DT_LONG];
}

+ (float) readFloat:(IOBuffer *)ibuffer {
    return [ibuffer readFloat];
}

+ (NSMutableArray *) readFloatArray:(IOBuffer *)ibuffer {
    return [MessageBuffer readArray:ibuffer :DT_FLOAT];

}

+ (double) readDouble:(IOBuffer *)ibuffer {
    return [ibuffer readDouble];
}

+ (NSMutableArray *) readDoubleArray:(IOBuffer *)ibuffer {
    return [MessageBuffer readArray:ibuffer :DT_DOUBLE];

}

+ (NSString *) readString:(IOBuffer *)ibuffer {
    if ([MessageBuffer readIsNull:ibuffer]) {
        return nil;
    } else {
        return [ibuffer readUTF];
    }
}

+ (NSMutableArray *) readStringArray:(IOBuffer *)ibuffer {
    return [MessageBuffer readArray:ibuffer :DT_STRING];
}

+ (void) writeIsNull:(IOBuffer *)obuffer :(NSObject *)obj {
    if (obj == nil) {
        [obuffer writeByte:MB_NULL];
    } else {
        [obuffer writeByte:MB_NOT_NULL];
    }
}

+ (BOOL) readIsNull:(IOBuffer *)ibuffer {
	return [ibuffer readByte] ? TRUE : FALSE;
}

+ (void) writeByte:(IOBuffer *)obuffer :(Byte)v {
    [obuffer writeByte:v];
}

+ (void) writeByteArray:(IOBuffer *)obuffer :(NSMutableArray *)ary {
    [MessageBuffer writeArray:obuffer :ary :DT_BYTE];
}

+ (void) writeBoolean:(IOBuffer *)obuffer :(BOOL)v {
	[obuffer writeBoolean:v];
}

+ (void) writeBooleanArray:(IOBuffer *)obuffer :(NSMutableArray *)ary {
	[MessageBuffer writeArray:obuffer :ary :DT_BOOL];
}

+ (void) writeShort:(IOBuffer *)obuffer :(short)v {
    [obuffer writeShort:v];
}

+ (void) writeShortArray:(IOBuffer *)obuffer :(NSMutableArray *)ary {
    [MessageBuffer writeArray:obuffer :ary :DT_SHORT];
}

+ (void) writeInteger:(IOBuffer *)obuffer :(int)v {
    [obuffer writeInt:v];
}

+ (void) writeIntegerArray:(IOBuffer *)obuffer :(NSMutableArray *)ary {
    [MessageBuffer writeArray:obuffer :ary :DT_INT];
}

+ (void) writeLong:(IOBuffer *)obuffer :(long long)v {
    [obuffer writeLong:v];
}

+ (void) writeLongArray:(IOBuffer *)obuffer :(NSMutableArray *)ary {
    [MessageBuffer writeArray:obuffer :ary :DT_LONG];
}

+ (void) writeFloat:(IOBuffer *)obuffer :(float)v {
    [obuffer writeFloat:v];
}

+ (void) writeFloatArray:(IOBuffer *)obuffer :(NSMutableArray *)ary {
    [MessageBuffer writeArray:obuffer :ary :DT_FLOAT];
}

+ (void) writeDouble:(IOBuffer *)obuffer :(double)v {
    [obuffer writeDouble:v];
}

+ (void) writeDoubleArray:(IOBuffer *)obuffer :(NSMutableArray *)ary {
    [MessageBuffer writeArray:obuffer :ary :DT_DOUBLE];
}

+ (void) writeString:(IOBuffer *)obuffer :(NSString *)v {
    [MessageBuffer writeIsNull:obuffer :v];
    if (v == nil) {
        return;
    }
    
    [obuffer writeUTF:v];
}

+ (void) writeStringArray:(IOBuffer *)obuffer :(NSMutableArray *)ary {
    [MessageBuffer writeArray:obuffer :ary :DT_STRING];
}

@end

@implementation NullMessageBean
- (void) printToString:(NSMutableString *)string {
    [string appendString:@"NullMessageBean {}"];
}
@end

///////////////////////////////////////////////////////////////////////////////
// 消息Prop
// Message Compiler 测试用例
// 道具
//
@implementation Prop

@synthesize ID;
@synthesize name;
@synthesize type;
@synthesize icon;
@synthesize quantity;
@synthesize image;

- (void) setName:(NSString *)str {
	if (self->name != nil) {
		[self->name release];
		self->name = nil;
	}
	
	if (str != nil) {
		self->name = [[NSString alloc] initWithString:str];
	}
}

- (void) setNameWithCString:(const char *)str {
	if (self->name != nil) {
		[self->name release];
		self->name = nil;
	}
	
	if (str != nil) {
		self->name = [[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding];
	}
}

- (NSUInteger) getNameToCString:(char *)str :(int) maxLen {
	if (self->name != nil) {
    	[self->name getCString:str maxLength:maxLen encoding:NSUTF8StringEncoding];
		return [self->name length];
	} else {
		return 0;
	}
}

- (void) addElementToImage:(Byte) elem {
	if (self->image == nil) {
		self->image = [[NSMutableArray alloc] initWithCapacity:16];
	}

	[self->image addObject:[NSNumber numberWithChar:elem]];
}

- (void) setImage:(NSMutableArray *) array {
	if (self->image != nil) {
		[self->image removeAllObjects];
		[self->image release];
		self->image = nil;
	}

	if (array != nil) {
		self->image = [[NSMutableArray alloc] initWithArray: array];
	}
}

- (void) setImageWithArray:(Byte *) ary :(int) len {
	if (self->image != nil) {
		[self->image removeAllObjects];
	} else {
    	self->image = [[NSMutableArray alloc] initWithCapacity:len];
    }
	
	if (ary == nil || len <= 0) {
		return;
	}
    
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
		number = [NSNumber numberWithChar:ary[i]];
        [self->image addObject:number];
    }
}

- (int) getImageToArray:(Byte *) ary {
	if (self->image == nil) {
		return 0;
	}
	
	int len = (int) [self->image count];
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
        number = [self->image objectAtIndex:i];
        ary[i] = [number charValue];
    }
	
	return len;
}


- (void) clear {
	[super clear];
	self->ID = 0;
	self->name = nil;
	self->type = 0x00;
	self->icon = 0;
	self->quantity = 0;
	self->image = nil;
}

/*
 * 把各field的值设为0或者nil
 */
- (Prop *) init {
	self = [super init];
	[self clear];
	return self;
}

/*
 * 释放消息Prop占用的内存
 */
- (void) dealloc {
#ifdef DEBUG
	NSLog(@"**** MessageBean Prop dealloc");
#endif

	if (self->name != nil) {
		[self->name release];
		self->name = nil;
	}
	
	if (self->image != nil) {
		[self->image removeAllObjects];
		[self->image release];
		self->image = nil;
	}
	
	[super dealloc];
}

/*
 * 把消息Prop的信息完整地格式化输出到字符串中。
 *
 * @param string 用于格式化输出结果的字符串对象。
 * @param indentLevel 输出时的缩进级别
 */
- (void) printToStringWithIndent:(NSMutableString *)string :(int)indentLevel {
	[string appendFormat:@"Prop [retainCount=%u] {\n", [self retainCount]];

	// 输出域 id
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"int ID = "];
	[string appendFormat:@"%d\n", self->ID];

	// 输出域 name
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"String name = "];
	if (self->name == nil) {
		[string appendString:@"nil\n"];
	} else {
		[string appendFormat:@"[retainCount=%u] \"%@\"\n", [self->name retainCount], self->name];
	}

	// 输出域 type
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"byte type = "];
	[string appendFormat:@"0X%02X\n", self->type]; 

	// 输出域 icon
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"short icon = "];
	[string appendFormat:@"%hi\n", self->icon];

	// 输出域 quantity
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"long quantity = "];
	[string appendFormat:@"%qi\n", self->quantity];

	// 输出域 image
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"byte[] image = "];
	if (self->image == nil) {
		[string appendString:@"nil\n"];
	} else {
		[string appendFormat:@"[retainCount=%u] {\n", [image retainCount]];
		int len = (int) [self->image count];
		NSNumber *number;
		for (int i = 0; i < len; ++i) {
			[self appendIndent:string :indentLevel + 2];
			[string appendFormat:@"[%d] = ", i];
			number = [self->image objectAtIndex:i];
			[string appendFormat:@"%@\n", number];
		}
		[self appendIndent:string :indentLevel + 1];
		[string appendString:@"}\n\n"];
	}


	[self appendIndent:string :indentLevel];
	[string appendString:@"}\n\n"];
}

/*
 * 把消息Prop的各域序列化到IOBuffer中。
 *
 * @param obuffer 用于存放序列化结果字节流的buffer对象
 */
- (void) seriallize:(IOBuffer *)obuffer {
	char *__currentFieldName = "N/A";
	@try {
		// 序列化域 id
		__currentFieldName = "id";
		[MessageBuffer writeInteger:obuffer :self->ID];

		// 序列化域 name
		__currentFieldName = "name";
		[MessageBuffer writeString:obuffer :self->name];

		// 序列化域 type
		__currentFieldName = "type";
		[MessageBuffer writeByte:obuffer :self->type];

		// 序列化域 icon
		__currentFieldName = "icon";
		[MessageBuffer writeShort:obuffer :self->icon];

		// 序列化域 quantity
		__currentFieldName = "quantity";
		[MessageBuffer writeLong:obuffer :self->quantity];

		// 序列化域 image
		__currentFieldName = "image";
		[MessageBuffer writeByteArray:obuffer :self->image];

	} @catch (NSException *__ex) {
		@throw [MessageBuffer createException:@"AllType#seriallize"
                                             :__ex
                                             :__FILE__
                                             :__LINE__
                                             :"Field=%s", __currentFieldName];
	}
}

/*
 * 从IOBuffer中反序列化出Prop各field的值。
 *
 * @param IOBuffer 储存数据流的buffer对象
 */
- (void) deseriallize:(IOBuffer *)ibuffer {
	char *__currentFieldName = "N/A";
	@try {
		// 反序列化域 id
		__currentFieldName = "id";
		self->ID = [MessageBuffer readInteger:ibuffer];

		// 反序列化域 name
		__currentFieldName = "name";
		self->name = [MessageBuffer readString:ibuffer];

		// 反序列化域 type
		__currentFieldName = "type";
		self->type = [MessageBuffer readByte:ibuffer];

		// 反序列化域 icon
		__currentFieldName = "icon";
		self->icon = [MessageBuffer readShort:ibuffer];

		// 反序列化域 quantity
		__currentFieldName = "quantity";
		self->quantity = [MessageBuffer readLong:ibuffer];

		// 反序列化域 image
		__currentFieldName = "image";
		self->image = [MessageBuffer readByteArray:ibuffer];

	} @catch (NSException *__ex) {
		@throw [MessageBuffer createException:@"AllType#deseriallize"
                                             :__ex
                                             :__FILE__
                                             :__LINE__
                                             :"Field=%s", __currentFieldName];
	}
}

@end

///////////////////////////////////////////////////////////////////////////////
// 消息Backpack
// 玩家背包
// 服务器下发玩家背包中的详细物品列表
//
@implementation Backpack

@synthesize asciiString;
@synthesize chineseString;
@synthesize characterId;
@synthesize props;

- (void) setAsciiString:(NSString *)str {
	if (self->asciiString != nil) {
		[self->asciiString release];
		self->asciiString = nil;
	}
	
	if (str != nil) {
		self->asciiString = [[NSString alloc] initWithString:str];
	}
}

- (void) setAsciiStringWithCString:(const char *)str {
	if (self->asciiString != nil) {
		[self->asciiString release];
		self->asciiString = nil;
	}
	
	if (str != nil) {
		self->asciiString = [[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding];
	}
}

- (NSUInteger) getAsciiStringToCString:(char *)str :(int) maxLen {
	if (self->asciiString != nil) {
    	[self->asciiString getCString:str maxLength:maxLen encoding:NSUTF8StringEncoding];
		return [self->asciiString length];
	} else {
		return 0;
	}
}

- (void) setChineseString:(NSString *)str {
	if (self->chineseString != nil) {
		[self->chineseString release];
		self->chineseString = nil;
	}
	
	if (str != nil) {
		self->chineseString = [[NSString alloc] initWithString:str];
	}
}

- (void) setChineseStringWithCString:(const char *)str {
	if (self->chineseString != nil) {
		[self->chineseString release];
		self->chineseString = nil;
	}
	
	if (str != nil) {
		self->chineseString = [[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding];
	}
}

- (NSUInteger) getChineseStringToCString:(char *)str :(int) maxLen {
	if (self->chineseString != nil) {
    	[self->chineseString getCString:str maxLength:maxLen encoding:NSUTF8StringEncoding];
		return [self->chineseString length];
	} else {
		return 0;
	}
}

- (void) addElementToProps:(Prop*) elem {
	if (self->props == nil) {
		self->props = [[NSMutableArray alloc] initWithCapacity:16];
	}

	[self->props addObject:elem];
}

- (void) setProps:(NSMutableArray *) array {
	if (self->props != nil) {
		[self->props removeAllObjects];
		[self->props release];
		self->props = nil;
	}

	if (array != nil) {
		self->props = [[NSMutableArray alloc] initWithArray: array];
	}
}


- (void) clear {
	[super clear];
	self->asciiString = nil;
	self->chineseString = nil;
	self->characterId = 0;
	self->props = nil;
}

/*
 * 把各field的值设为0或者nil
 */
- (Backpack *) init {
	self = [super init];
	[self clear];
	return self;
}

/*
 * 释放消息Backpack占用的内存
 */
- (void) dealloc {
#ifdef DEBUG
	NSLog(@"**** MessageBean Backpack dealloc");
#endif

	if (self->asciiString != nil) {
		[self->asciiString release];
		self->asciiString = nil;
	}
	
	if (self->chineseString != nil) {
		[self->chineseString release];
		self->chineseString = nil;
	}
	
	if (self->props != nil) {
		[self->props removeAllObjects];
		[self->props release];
		self->props = nil;
	}
	
	[super dealloc];
}

/*
 * 把消息Backpack的信息完整地格式化输出到字符串中。
 *
 * @param string 用于格式化输出结果的字符串对象。
 * @param indentLevel 输出时的缩进级别
 */
- (void) printToStringWithIndent:(NSMutableString *)string :(int)indentLevel {
	[string appendFormat:@"Backpack [retainCount=%u] {\n", [self retainCount]];

	// 输出域 asciiString
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"String asciiString = "];
	if (self->asciiString == nil) {
		[string appendString:@"nil\n"];
	} else {
		[string appendFormat:@"[retainCount=%u] \"%@\"\n", [self->asciiString retainCount], self->asciiString];
	}

	// 输出域 chineseString
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"String chineseString = "];
	if (self->chineseString == nil) {
		[string appendString:@"nil\n"];
	} else {
		[string appendFormat:@"[retainCount=%u] \"%@\"\n", [self->chineseString retainCount], self->chineseString];
	}

	// 输出域 characterId
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"int characterId = "];
	[string appendFormat:@"%d\n", self->characterId];

	// 输出域 props
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"List<Prop> props = "];
	if (self->props == nil) {
		[string appendString:@"nil\n"];
	} else {
		[string appendFormat:@"[retainCount=%u] {\n", [props retainCount]];
		int len = (int) [self->props count];
		MessageBean *bean;
		for (int i = 0; i < len; ++i) {
			[self appendIndent:string :indentLevel + 2];
			[string appendFormat:@"<%d> = ", i];
			bean = [self->props objectAtIndex:i];
			[bean printToStringWithIndent:string :indentLevel +2];
		}
		[self appendIndent:string :indentLevel + 1];
		[string appendString:@"}\n\n"];
	}


	[self appendIndent:string :indentLevel];
	[string appendString:@"}\n\n"];
}

/*
 * 把消息Backpack的各域序列化到IOBuffer中。
 *
 * @param obuffer 用于存放序列化结果字节流的buffer对象
 */
- (void) seriallize:(IOBuffer *)obuffer {
	char *__currentFieldName = "N/A";
	@try {
		// 序列化域 asciiString
		__currentFieldName = "asciiString";
		[MessageBuffer writeString:obuffer :self->asciiString];

		// 序列化域 chineseString
		__currentFieldName = "chineseString";
		[MessageBuffer writeString:obuffer :self->chineseString];

		// 序列化域 characterId
		__currentFieldName = "characterId";
		[MessageBuffer writeInteger:obuffer :self->characterId];

		// 序列化域 props
		__currentFieldName = "props";
		[MessageBuffer packBeanList:obuffer :self->props];

	} @catch (NSException *__ex) {
		@throw [MessageBuffer createException:@"AllType#seriallize"
                                             :__ex
                                             :__FILE__
                                             :__LINE__
                                             :"Field=%s", __currentFieldName];
	}
}

/*
 * 从IOBuffer中反序列化出Backpack各field的值。
 *
 * @param IOBuffer 储存数据流的buffer对象
 */
- (void) deseriallize:(IOBuffer *)ibuffer {
	char *__currentFieldName = "N/A";
	@try {
		// 反序列化域 asciiString
		__currentFieldName = "asciiString";
		self->asciiString = [MessageBuffer readString:ibuffer];

		// 反序列化域 chineseString
		__currentFieldName = "chineseString";
		self->chineseString = [MessageBuffer readString:ibuffer];

		// 反序列化域 characterId
		__currentFieldName = "characterId";
		self->characterId = [MessageBuffer readInteger:ibuffer];

		// 反序列化域 props
		__currentFieldName = "props";
		self->props = [MessageBuffer unpackBeanList:ibuffer :[Prop class]];

	} @catch (NSException *__ex) {
		@throw [MessageBuffer createException:@"AllType#deseriallize"
                                             :__ex
                                             :__FILE__
                                             :__LINE__
                                             :"Field=%s", __currentFieldName];
	}
}

@end

///////////////////////////////////////////////////////////////////////////////
// 消息AllType
// Message Compiler 测试用例
// 测试所有可能的数据类型
//
@implementation AllType

@synthesize byteValue;
@synthesize byteArray;
@synthesize boolValue;
@synthesize boolArray;
@synthesize shortValue;
@synthesize shortArray;
@synthesize intValue;
@synthesize intArray;
@synthesize longValue;
@synthesize longArray;
@synthesize floatValue;
@synthesize floatArray;
@synthesize doubleValue;
@synthesize doubleArray;
@synthesize stringValue;
@synthesize stringArray;
@synthesize prop;
@synthesize propList;

- (void) addElementToByteArray:(Byte) elem {
	if (self->byteArray == nil) {
		self->byteArray = [[NSMutableArray alloc] initWithCapacity:16];
	}

	[self->byteArray addObject:[NSNumber numberWithChar:elem]];
}

- (void) setByteArray:(NSMutableArray *) array {
	if (self->byteArray != nil) {
		[self->byteArray removeAllObjects];
		[self->byteArray release];
		self->byteArray = nil;
	}

	if (array != nil) {
		self->byteArray = [[NSMutableArray alloc] initWithArray: array];
	}
}

- (void) setByteArrayWithArray:(Byte *) ary :(int) len {
	if (self->byteArray != nil) {
		[self->byteArray removeAllObjects];
	} else {
    	self->byteArray = [[NSMutableArray alloc] initWithCapacity:len];
    }
	
	if (ary == nil || len <= 0) {
		return;
	}
    
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
		number = [NSNumber numberWithChar:ary[i]];
        [self->byteArray addObject:number];
    }
}

- (int) getByteArrayToArray:(Byte *) ary {
	if (self->byteArray == nil) {
		return 0;
	}
	
	int len = (int) [self->byteArray count];
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
        number = [self->byteArray objectAtIndex:i];
        ary[i] = [number charValue];
    }
	
	return len;
}

- (void) addElementToBoolArray:(BOOL) elem {
	if (self->boolArray == nil) {
		self->boolArray = [[NSMutableArray alloc] initWithCapacity:16];
	}

	[self->boolArray addObject:[NSNumber numberWithBool:elem]];
}

- (void) setBoolArray:(NSMutableArray *) array {
	if (self->boolArray != nil) {
		[self->boolArray removeAllObjects];
		[self->boolArray release];
		self->boolArray = nil;
	}

	if (array != nil) {
		self->boolArray = [[NSMutableArray alloc] initWithArray: array];
	}
}

- (void) setBoolArrayWithArray:(BOOL *) ary :(int) len {
	if (self->boolArray != nil) {
		[self->boolArray removeAllObjects];
	} else {
    	self->boolArray = [[NSMutableArray alloc] initWithCapacity:len];
    }
	
	if (ary == nil || len <= 0) {
		return;
	}
    
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
		number = [NSNumber numberWithBool:ary[i]];
        [self->boolArray addObject:number];
    }
}

- (int) getBoolArrayToArray:(BOOL *) ary {
	if (self->boolArray == nil) {
		return 0;
	}
	
	int len = (int) [self->boolArray count];
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
        number = [self->boolArray objectAtIndex:i];
		ary[i] = [number boolValue];
    }
	
	return len;
}

- (void) addElementToShortArray:(short) elem {
	if (self->shortArray == nil) {
		self->shortArray = [[NSMutableArray alloc] initWithCapacity:16];
	}

	[self->shortArray addObject:[NSNumber numberWithShort:elem]];
}

- (void) setShortArray:(NSMutableArray *) array {
	if (self->shortArray != nil) {
		[self->shortArray removeAllObjects];
		[self->shortArray release];
		self->shortArray = nil;
	}

	if (array != nil) {
		self->shortArray = [[NSMutableArray alloc] initWithArray: array];
	}
}

- (void) setShortArrayWithArray:(short *) ary :(int) len {
	if (self->shortArray != nil) {
		[self->shortArray removeAllObjects];
	} else {
    	self->shortArray = [[NSMutableArray alloc] initWithCapacity:len];
    }
	
	if (ary == nil || len <= 0) {
		return;
	}
    
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
		number = [NSNumber numberWithShort:ary[i]];
        [self->shortArray addObject:number];
    }
}

- (int) getShortArrayToArray:(short *) ary {
	if (self->shortArray == nil) {
		return 0;
	}
	
	int len = (int) [self->shortArray count];
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
        number = [self->shortArray objectAtIndex:i];
		ary[i] = [number shortValue];
    }
	
	return len;
}

- (void) addElementToIntArray:(int) elem {
	if (self->intArray == nil) {
		self->intArray = [[NSMutableArray alloc] initWithCapacity:16];
	}

	[self->intArray addObject:[NSNumber numberWithInt:elem]];
}

- (void) setIntArray:(NSMutableArray *) array {
	if (self->intArray != nil) {
		[self->intArray removeAllObjects];
		[self->intArray release];
		self->intArray = nil;
	}

	if (array != nil) {
		self->intArray = [[NSMutableArray alloc] initWithArray: array];
	}
}

- (void) setIntArrayWithArray:(int *) ary :(int) len {
	if (self->intArray != nil) {
		[self->intArray removeAllObjects];
	} else {
    	self->intArray = [[NSMutableArray alloc] initWithCapacity:len];
    }
	
	if (ary == nil || len <= 0) {
		return;
	}
    
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
		number = [NSNumber numberWithInt:ary[i]];
        [self->intArray addObject:number];
    }
}

- (int) getIntArrayToArray:(int *) ary {
	if (self->intArray == nil) {
		return 0;
	}
	
	int len = (int) [self->intArray count];
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
        number = [self->intArray objectAtIndex:i];
		ary[i] = [number intValue];
    }
	
	return len;
}

- (void) addElementToLongArray:(long long) elem {
	if (self->longArray == nil) {
		self->longArray = [[NSMutableArray alloc] initWithCapacity:16];
	}

	[self->longArray addObject:[NSNumber numberWithLongLong:elem]];
}

- (void) setLongArray:(NSMutableArray *) array {
	if (self->longArray != nil) {
		[self->longArray removeAllObjects];
		[self->longArray release];
		self->longArray = nil;
	}

	if (array != nil) {
		self->longArray = [[NSMutableArray alloc] initWithArray: array];
	}
}

- (void) setLongArrayWithArray:(long long *) ary :(int) len {
	if (self->longArray != nil) {
		[self->longArray removeAllObjects];
	} else {
    	self->longArray = [[NSMutableArray alloc] initWithCapacity:len];
    }
	
	if (ary == nil || len <= 0) {
		return;
	}
    
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
		number = [NSNumber numberWithLongLong:ary[i]];
        [self->longArray addObject:number];
    }
}

- (int) getLongArrayToArray:(long long *) ary {
	if (self->longArray == nil) {
		return 0;
	}
	
	int len = (int) [self->longArray count];
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
        number = [self->longArray objectAtIndex:i];
		ary[i] = [number longLongValue];
    }
	
	return len;
}

- (void) addElementToFloatArray:(float) elem {
	if (self->floatArray == nil) {
		self->floatArray = [[NSMutableArray alloc] initWithCapacity:16];
	}

	[self->floatArray addObject:[NSNumber numberWithFloat:elem]];
}

- (void) setFloatArray:(NSMutableArray *) array {
	if (self->floatArray != nil) {
		[self->floatArray removeAllObjects];
		[self->floatArray release];
		self->floatArray = nil;
	}

	if (array != nil) {
		self->floatArray = [[NSMutableArray alloc] initWithArray: array];
	}
}

- (void) setFloatArrayWithArray:(float *) ary :(int) len {
	if (self->floatArray != nil) {
		[self->floatArray removeAllObjects];
	} else {
    	self->floatArray = [[NSMutableArray alloc] initWithCapacity:len];
    }
	
	if (ary == nil || len <= 0) {
		return;
	}
    
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
		number = [NSNumber numberWithFloat:ary[i]];
        [self->floatArray addObject:number];
    }
}

- (int) getFloatArrayToArray:(float *) ary {
	if (self->floatArray == nil) {
		return 0;
	}
	
	int len = (int) [self->floatArray count];
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
        number = [self->floatArray objectAtIndex:i];
		ary[i] = [number floatValue];
    }
	
	return len;
}

- (void) addElementToDoubleArray:(double) elem {
	if (self->doubleArray == nil) {
		self->doubleArray = [[NSMutableArray alloc] initWithCapacity:16];
	}

	[self->doubleArray addObject:[NSNumber numberWithDouble:elem]];
}

- (void) setDoubleArray:(NSMutableArray *) array {
	if (self->doubleArray != nil) {
		[self->doubleArray removeAllObjects];
		[self->doubleArray release];
		self->doubleArray = nil;
	}

	if (array != nil) {
		self->doubleArray = [[NSMutableArray alloc] initWithArray: array];
	}
}

- (void) setDoubleArrayWithArray:(double *) ary :(int) len {
	if (self->doubleArray != nil) {
		[self->doubleArray removeAllObjects];
	} else {
    	self->doubleArray = [[NSMutableArray alloc] initWithCapacity:len];
    }
	
	if (ary == nil || len <= 0) {
		return;
	}
    
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
		number = [NSNumber numberWithDouble:ary[i]];
        [self->doubleArray addObject:number];
    }
}

- (int) getDoubleArrayToArray:(double *) ary {
	if (self->doubleArray == nil) {
		return 0;
	}
	
	int len = (int) [self->doubleArray count];
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
        number = [self->doubleArray objectAtIndex:i];
		ary[i] = [number doubleValue];
    }
	
	return len;
}

- (void) setStringValue:(NSString *)str {
	if (self->stringValue != nil) {
		[self->stringValue release];
		self->stringValue = nil;
	}
	
	if (str != nil) {
		self->stringValue = [[NSString alloc] initWithString:str];
	}
}

- (void) setStringValueWithCString:(const char *)str {
	if (self->stringValue != nil) {
		[self->stringValue release];
		self->stringValue = nil;
	}
	
	if (str != nil) {
		self->stringValue = [[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding];
	}
}

- (NSUInteger) getStringValueToCString:(char *)str :(int) maxLen {
	if (self->stringValue != nil) {
    	[self->stringValue getCString:str maxLength:maxLen encoding:NSUTF8StringEncoding];
		return [self->stringValue length];
	} else {
		return 0;
	}
}

- (void) addElementToStringArray:(NSString*) elem {
	if (self->stringArray == nil) {
		self->stringArray = [[NSMutableArray alloc] initWithCapacity:16];
	}

	[self->stringArray addObject:elem];
}

- (void) setStringArray:(NSMutableArray *) array {
	if (self->stringArray != nil) {
		[self->stringArray removeAllObjects];
		[self->stringArray release];
		self->stringArray = nil;
	}

	if (array != nil) {
		self->stringArray = [[NSMutableArray alloc] initWithArray: array];
	}
}

- (void) setProp:(Prop *) ref {
	if (self->prop != nil) {
		[self->prop release];
		self->prop = nil;
	}
	
	if (ref != nil) {
		[ref retain];
		self->prop = ref;
	}
}

- (void) addElementToPropList:(Prop*) elem {
	if (self->propList == nil) {
		self->propList = [[NSMutableArray alloc] initWithCapacity:16];
	}

	[self->propList addObject:elem];
}

- (void) setPropList:(NSMutableArray *) array {
	if (self->propList != nil) {
		[self->propList removeAllObjects];
		[self->propList release];
		self->propList = nil;
	}

	if (array != nil) {
		self->propList = [[NSMutableArray alloc] initWithArray: array];
	}
}


- (void) clear {
	[super clear];
	self->byteValue = 0x00;
	self->byteArray = nil;
	self->boolValue = FALSE;
	self->boolArray = nil;
	self->shortValue = 0;
	self->shortArray = nil;
	self->intValue = 0;
	self->intArray = nil;
	self->longValue = 0;
	self->longArray = nil;
	self->floatValue = 0.00;
	self->floatArray = nil;
	self->doubleValue = 0.00;
	self->doubleArray = nil;
	self->stringValue = nil;
	self->stringArray = nil;
	self->prop = nil;
	self->propList = nil;
}

/*
 * 把各field的值设为0或者nil
 */
- (AllType *) init {
	self = [super init];
	[self clear];
	return self;
}

/*
 * 释放消息AllType占用的内存
 */
- (void) dealloc {
#ifdef DEBUG
	NSLog(@"**** MessageBean AllType dealloc");
#endif

	if (self->byteArray != nil) {
		[self->byteArray removeAllObjects];
		[self->byteArray release];
		self->byteArray = nil;
	}
	
	if (self->boolArray != nil) {
		[self->boolArray removeAllObjects];
		[self->boolArray release];
		self->boolArray = nil;
	}
	
	if (self->shortArray != nil) {
		[self->shortArray removeAllObjects];
		[self->shortArray release];
		self->shortArray = nil;
	}
	
	if (self->intArray != nil) {
		[self->intArray removeAllObjects];
		[self->intArray release];
		self->intArray = nil;
	}
	
	if (self->longArray != nil) {
		[self->longArray removeAllObjects];
		[self->longArray release];
		self->longArray = nil;
	}
	
	if (self->floatArray != nil) {
		[self->floatArray removeAllObjects];
		[self->floatArray release];
		self->floatArray = nil;
	}
	
	if (self->doubleArray != nil) {
		[self->doubleArray removeAllObjects];
		[self->doubleArray release];
		self->doubleArray = nil;
	}
	
	if (self->stringValue != nil) {
		[self->stringValue release];
		self->stringValue = nil;
	}
	
	if (self->stringArray != nil) {
		[self->stringArray removeAllObjects];
		[self->stringArray release];
		self->stringArray = nil;
	}
	
	if (self->prop != nil) {
		[self->prop release];
		self->prop = nil;
	}
	
	if (self->propList != nil) {
		[self->propList removeAllObjects];
		[self->propList release];
		self->propList = nil;
	}
	
	[super dealloc];
}

/*
 * 把消息AllType的信息完整地格式化输出到字符串中。
 *
 * @param string 用于格式化输出结果的字符串对象。
 * @param indentLevel 输出时的缩进级别
 */
- (void) printToStringWithIndent:(NSMutableString *)string :(int)indentLevel {
	[string appendFormat:@"AllType [retainCount=%u] {\n", [self retainCount]];

	// 输出域 byteValue
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"byte byteValue = "];
	[string appendFormat:@"0X%02X\n", self->byteValue]; 

	// 输出域 byteArray
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"byte[] byteArray = "];
	if (self->byteArray == nil) {
		[string appendString:@"nil\n"];
	} else {
		[string appendFormat:@"[retainCount=%u] {\n", [byteArray retainCount]];
		int len = (int) [self->byteArray count];
		NSNumber *number;
		for (int i = 0; i < len; ++i) {
			[self appendIndent:string :indentLevel + 2];
			[string appendFormat:@"[%d] = ", i];
			number = [self->byteArray objectAtIndex:i];
			[string appendFormat:@"%@\n", number];
		}
		[self appendIndent:string :indentLevel + 1];
		[string appendString:@"}\n\n"];
	}

	// 输出域 boolValue
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"boolean boolValue = "];
	[string appendFormat:@"%s\n", self->boolValue ? "TRUE" : "FALSE"];

	// 输出域 boolArray
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"boolean[] boolArray = "];
	if (self->boolArray == nil) {
		[string appendString:@"nil\n"];
	} else {
		[string appendFormat:@"[retainCount=%u] {\n", [boolArray retainCount]];
		int len = (int) [self->boolArray count];
		NSNumber *number;
		for (int i = 0; i < len; ++i) {
			[self appendIndent:string :indentLevel + 2];
			[string appendFormat:@"[%d] = ", i];
			number = [self->boolArray objectAtIndex:i];
			[string appendFormat:@"%s\n", [number boolValue] ? "TRUE" : "FALSE"];
		}
		[self appendIndent:string :indentLevel + 1];
		[string appendString:@"}\n\n"];
	}

	// 输出域 shortValue
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"short shortValue = "];
	[string appendFormat:@"%hi\n", self->shortValue];

	// 输出域 shortArray
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"short[] shortArray = "];
	if (self->shortArray == nil) {
		[string appendString:@"nil\n"];
	} else {
		[string appendFormat:@"[retainCount=%u] {\n", [shortArray retainCount]];
		int len = (int) [self->shortArray count];
		NSNumber *number;
		for (int i = 0; i < len; ++i) {
			[self appendIndent:string :indentLevel + 2];
			[string appendFormat:@"[%d] = ", i];
			number = [self->shortArray objectAtIndex:i];
			[string appendFormat:@"%@\n", number];
		}
		[self appendIndent:string :indentLevel + 1];
		[string appendString:@"}\n\n"];
	}

	// 输出域 intValue
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"int intValue = "];
	[string appendFormat:@"%d\n", self->intValue];

	// 输出域 intArray
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"int[] intArray = "];
	if (self->intArray == nil) {
		[string appendString:@"nil\n"];
	} else {
		[string appendFormat:@"[retainCount=%u] {\n", [intArray retainCount]];
		int len = (int) [self->intArray count];
		NSNumber *number;
		for (int i = 0; i < len; ++i) {
			[self appendIndent:string :indentLevel + 2];
			[string appendFormat:@"[%d] = ", i];
			number = [self->intArray objectAtIndex:i];
			[string appendFormat:@"%@\n", number];
		}
		[self appendIndent:string :indentLevel + 1];
		[string appendString:@"}\n\n"];
	}

	// 输出域 longValue
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"long longValue = "];
	[string appendFormat:@"%qi\n", self->longValue];

	// 输出域 longArray
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"long[] longArray = "];
	if (self->longArray == nil) {
		[string appendString:@"nil\n"];
	} else {
		[string appendFormat:@"[retainCount=%u] {\n", [longArray retainCount]];
		int len = (int) [self->longArray count];
		NSNumber *number;
		for (int i = 0; i < len; ++i) {
			[self appendIndent:string :indentLevel + 2];
			[string appendFormat:@"[%d] = ", i];
			number = [self->longArray objectAtIndex:i];
			[string appendFormat:@"%@\n", number];
		}
		[self appendIndent:string :indentLevel + 1];
		[string appendString:@"}\n\n"];
	}

	// 输出域 floatValue
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"float floatValue = "];
	[string appendFormat:@"%f\n", self->floatValue];

	// 输出域 floatArray
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"float[] floatArray = "];
	if (self->floatArray == nil) {
		[string appendString:@"nil\n"];
	} else {
		[string appendFormat:@"[retainCount=%u] {\n", [floatArray retainCount]];
		int len = (int) [self->floatArray count];
		NSNumber *number;
		for (int i = 0; i < len; ++i) {
			[self appendIndent:string :indentLevel + 2];
			[string appendFormat:@"[%d] = ", i];
			number = [self->floatArray objectAtIndex:i];
			[string appendFormat:@"%@\n", number];
		}
		[self appendIndent:string :indentLevel + 1];
		[string appendString:@"}\n\n"];
	}

	// 输出域 doubleValue
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"double doubleValue = "];
	[string appendFormat:@"%f\n", self->doubleValue];

	// 输出域 doubleArray
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"double[] doubleArray = "];
	if (self->doubleArray == nil) {
		[string appendString:@"nil\n"];
	} else {
		[string appendFormat:@"[retainCount=%u] {\n", [doubleArray retainCount]];
		int len = (int) [self->doubleArray count];
		NSNumber *number;
		for (int i = 0; i < len; ++i) {
			[self appendIndent:string :indentLevel + 2];
			[string appendFormat:@"[%d] = ", i];
			number = [self->doubleArray objectAtIndex:i];
			[string appendFormat:@"%@\n", number];
		}
		[self appendIndent:string :indentLevel + 1];
		[string appendString:@"}\n\n"];
	}

	// 输出域 stringValue
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"String stringValue = "];
	if (self->stringValue == nil) {
		[string appendString:@"nil\n"];
	} else {
		[string appendFormat:@"[retainCount=%u] \"%@\"\n", [self->stringValue retainCount], self->stringValue];
	}

	// 输出域 stringArray
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"String[] stringArray = "];
	if (self->stringArray == nil) {
		[string appendString:@"nil\n"];
	} else {
		[string appendFormat:@"[retainCount=%u] {\n", [stringArray retainCount]];
		int len = (int) [self->stringArray count];
		NSNumber *number;
		for (int i = 0; i < len; ++i) {
			[self appendIndent:string :indentLevel + 2];
			[string appendFormat:@"[%d] = ", i];
			number = [self->stringArray objectAtIndex:i];
			[string appendFormat:@"[retainCount=%u] \"%@\"\n", [number retainCount], number];
		}
		[self appendIndent:string :indentLevel + 1];
		[string appendString:@"}\n\n"];
	}

	// 输出域 prop
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"Prop prop = "];
	if (self->prop == nil) {
		[string appendString:@"nil\n"];
	} else {
		[self->prop printToStringWithIndent:string :indentLevel + 1];
	}

	// 输出域 propList
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"List<Prop> propList = "];
	if (self->propList == nil) {
		[string appendString:@"nil\n"];
	} else {
		[string appendFormat:@"[retainCount=%u] {\n", [propList retainCount]];
		int len = (int) [self->propList count];
		MessageBean *bean;
		for (int i = 0; i < len; ++i) {
			[self appendIndent:string :indentLevel + 2];
			[string appendFormat:@"<%d> = ", i];
			bean = [self->propList objectAtIndex:i];
			[bean printToStringWithIndent:string :indentLevel +2];
		}
		[self appendIndent:string :indentLevel + 1];
		[string appendString:@"}\n\n"];
	}


	[self appendIndent:string :indentLevel];
	[string appendString:@"}\n\n"];
}

/*
 * 把消息AllType的各域序列化到IOBuffer中。
 *
 * @param obuffer 用于存放序列化结果字节流的buffer对象
 */
- (void) seriallize:(IOBuffer *)obuffer {
	char *__currentFieldName = "N/A";
	@try {
		// 序列化域 byteValue
		__currentFieldName = "byteValue";
		[MessageBuffer writeByte:obuffer :self->byteValue];

		// 序列化域 byteArray
		__currentFieldName = "byteArray";
		[MessageBuffer writeByteArray:obuffer :self->byteArray];

		// 序列化域 boolValue
		__currentFieldName = "boolValue";
		[MessageBuffer writeBoolean:obuffer :self->boolValue];

		// 序列化域 boolArray
		__currentFieldName = "boolArray";
		[MessageBuffer writeBooleanArray:obuffer :self->boolArray];

		// 序列化域 shortValue
		__currentFieldName = "shortValue";
		[MessageBuffer writeShort:obuffer :self->shortValue];

		// 序列化域 shortArray
		__currentFieldName = "shortArray";
		[MessageBuffer writeShortArray:obuffer :self->shortArray];

		// 序列化域 intValue
		__currentFieldName = "intValue";
		[MessageBuffer writeInteger:obuffer :self->intValue];

		// 序列化域 intArray
		__currentFieldName = "intArray";
		[MessageBuffer writeIntegerArray:obuffer :self->intArray];

		// 序列化域 longValue
		__currentFieldName = "longValue";
		[MessageBuffer writeLong:obuffer :self->longValue];

		// 序列化域 longArray
		__currentFieldName = "longArray";
		[MessageBuffer writeLongArray:obuffer :self->longArray];

		// 序列化域 floatValue
		__currentFieldName = "floatValue";
		[MessageBuffer writeFloat:obuffer :self->floatValue];

		// 序列化域 floatArray
		__currentFieldName = "floatArray";
		[MessageBuffer writeFloatArray:obuffer :self->floatArray];

		// 序列化域 doubleValue
		__currentFieldName = "doubleValue";
		[MessageBuffer writeDouble:obuffer :self->doubleValue];

		// 序列化域 doubleArray
		__currentFieldName = "doubleArray";
		[MessageBuffer writeDoubleArray:obuffer :self->doubleArray];

		// 序列化域 stringValue
		__currentFieldName = "stringValue";
		[MessageBuffer writeString:obuffer :self->stringValue];

		// 序列化域 stringArray
		__currentFieldName = "stringArray";
		[MessageBuffer writeStringArray:obuffer :self->stringArray];

		// 序列化域 prop
		__currentFieldName = "prop";
		[MessageBuffer packBean:obuffer :self->prop];

		// 序列化域 propList
		__currentFieldName = "propList";
		[MessageBuffer packBeanList:obuffer :self->propList];

	} @catch (NSException *__ex) {
		@throw [MessageBuffer createException:@"AllType#seriallize"
                                             :__ex
                                             :__FILE__
                                             :__LINE__
                                             :"Field=%s", __currentFieldName];
	}
}

/*
 * 从IOBuffer中反序列化出AllType各field的值。
 *
 * @param IOBuffer 储存数据流的buffer对象
 */
- (void) deseriallize:(IOBuffer *)ibuffer {
	char *__currentFieldName = "N/A";
	@try {
		// 反序列化域 byteValue
		__currentFieldName = "byteValue";
		self->byteValue = [MessageBuffer readByte:ibuffer];

		// 反序列化域 byteArray
		__currentFieldName = "byteArray";
		self->byteArray = [MessageBuffer readByteArray:ibuffer];

		// 反序列化域 boolValue
		__currentFieldName = "boolValue";
		self->boolValue = [MessageBuffer readBoolean:ibuffer];

		// 反序列化域 boolArray
		__currentFieldName = "boolArray";
		self->boolArray = [MessageBuffer readBooleanArray:ibuffer];

		// 反序列化域 shortValue
		__currentFieldName = "shortValue";
		self->shortValue = [MessageBuffer readShort:ibuffer];

		// 反序列化域 shortArray
		__currentFieldName = "shortArray";
		self->shortArray = [MessageBuffer readShortArray:ibuffer];

		// 反序列化域 intValue
		__currentFieldName = "intValue";
		self->intValue = [MessageBuffer readInteger:ibuffer];

		// 反序列化域 intArray
		__currentFieldName = "intArray";
		self->intArray = [MessageBuffer readIntegerArray:ibuffer];

		// 反序列化域 longValue
		__currentFieldName = "longValue";
		self->longValue = [MessageBuffer readLong:ibuffer];

		// 反序列化域 longArray
		__currentFieldName = "longArray";
		self->longArray = [MessageBuffer readLongArray:ibuffer];

		// 反序列化域 floatValue
		__currentFieldName = "floatValue";
		self->floatValue = [MessageBuffer readFloat:ibuffer];

		// 反序列化域 floatArray
		__currentFieldName = "floatArray";
		self->floatArray = [MessageBuffer readFloatArray:ibuffer];

		// 反序列化域 doubleValue
		__currentFieldName = "doubleValue";
		self->doubleValue = [MessageBuffer readDouble:ibuffer];

		// 反序列化域 doubleArray
		__currentFieldName = "doubleArray";
		self->doubleArray = [MessageBuffer readDoubleArray:ibuffer];

		// 反序列化域 stringValue
		__currentFieldName = "stringValue";
		self->stringValue = [MessageBuffer readString:ibuffer];

		// 反序列化域 stringArray
		__currentFieldName = "stringArray";
		self->stringArray = [MessageBuffer readStringArray:ibuffer];

		// 反序列化域 prop
		__currentFieldName = "prop";
		self->prop = (Prop *) [MessageBuffer unpackBean:ibuffer :[Prop class]];

		// 反序列化域 propList
		__currentFieldName = "propList";
		self->propList = [MessageBuffer unpackBeanList:ibuffer :[Prop class]];

	} @catch (NSException *__ex) {
		@throw [MessageBuffer createException:@"AllType#deseriallize"
                                             :__ex
                                             :__FILE__
                                             :__LINE__
                                             :"Field=%s", __currentFieldName];
	}
}

@end

///////////////////////////////////////////////////////////////////////////////
// 消息SceneMap
// 地图数据
//
@implementation SceneMap

@synthesize ID;
@synthesize data;
@synthesize prop;

- (void) addElementToData:(Byte) elem {
	if (self->data == nil) {
		self->data = [[NSMutableArray alloc] initWithCapacity:16];
	}

	[self->data addObject:[NSNumber numberWithChar:elem]];
}

- (void) setData:(NSMutableArray *) array {
	if (self->data != nil) {
		[self->data removeAllObjects];
		[self->data release];
		self->data = nil;
	}

	if (array != nil) {
		self->data = [[NSMutableArray alloc] initWithArray: array];
	}
}

- (void) setDataWithArray:(Byte *) ary :(int) len {
	if (self->data != nil) {
		[self->data removeAllObjects];
	} else {
    	self->data = [[NSMutableArray alloc] initWithCapacity:len];
    }
	
	if (ary == nil || len <= 0) {
		return;
	}
    
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
		number = [NSNumber numberWithChar:ary[i]];
        [self->data addObject:number];
    }
}

- (int) getDataToArray:(Byte *) ary {
	if (self->data == nil) {
		return 0;
	}
	
	int len = (int) [self->data count];
    NSNumber *number;
    for (int i = 0; i < len; ++i) {
        number = [self->data objectAtIndex:i];
        ary[i] = [number charValue];
    }
	
	return len;
}

- (void) setProp:(Prop *) ref {
	if (self->prop != nil) {
		[self->prop release];
		self->prop = nil;
	}
	
	if (ref != nil) {
		[ref retain];
		self->prop = ref;
	}
}


- (void) clear {
	[super clear];
	self->ID = 0;
	self->data = nil;
	self->prop = nil;
}

/*
 * 把各field的值设为0或者nil
 */
- (SceneMap *) init {
	self = [super init];
	[self clear];
	return self;
}

/*
 * 释放消息SceneMap占用的内存
 */
- (void) dealloc {
#ifdef DEBUG
	NSLog(@"**** MessageBean SceneMap dealloc");
#endif

	if (self->data != nil) {
		[self->data removeAllObjects];
		[self->data release];
		self->data = nil;
	}
	
	if (self->prop != nil) {
		[self->prop release];
		self->prop = nil;
	}
	
	[super dealloc];
}

/*
 * 把消息SceneMap的信息完整地格式化输出到字符串中。
 *
 * @param string 用于格式化输出结果的字符串对象。
 * @param indentLevel 输出时的缩进级别
 */
- (void) printToStringWithIndent:(NSMutableString *)string :(int)indentLevel {
	[string appendFormat:@"SceneMap [retainCount=%u] {\n", [self retainCount]];

	// 输出域 id
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"int ID = "];
	[string appendFormat:@"%d\n", self->ID];

	// 输出域 data
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"byte[] data = "];
	if (self->data == nil) {
		[string appendString:@"nil\n"];
	} else {
		[string appendFormat:@"[retainCount=%u] {\n", [data retainCount]];
		int len = (int) [self->data count];
		NSNumber *number;
		for (int i = 0; i < len; ++i) {
			[self appendIndent:string :indentLevel + 2];
			[string appendFormat:@"[%d] = ", i];
			number = [self->data objectAtIndex:i];
			[string appendFormat:@"%@\n", number];
		}
		[self appendIndent:string :indentLevel + 1];
		[string appendString:@"}\n\n"];
	}

	// 输出域 prop
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"Prop prop = "];
	if (self->prop == nil) {
		[string appendString:@"nil\n"];
	} else {
		[self->prop printToStringWithIndent:string :indentLevel + 1];
	}


	[self appendIndent:string :indentLevel];
	[string appendString:@"}\n\n"];
}

/*
 * 把消息SceneMap的各域序列化到IOBuffer中。
 *
 * @param obuffer 用于存放序列化结果字节流的buffer对象
 */
- (void) seriallize:(IOBuffer *)obuffer {
	char *__currentFieldName = "N/A";
	@try {
		// 序列化域 id
		__currentFieldName = "id";
		[MessageBuffer writeInteger:obuffer :self->ID];

		// 序列化域 data
		__currentFieldName = "data";
		[MessageBuffer writeByteArray:obuffer :self->data];

		// 序列化域 prop
		__currentFieldName = "prop";
		[MessageBuffer packBean:obuffer :self->prop];

	} @catch (NSException *__ex) {
		@throw [MessageBuffer createException:@"AllType#seriallize"
                                             :__ex
                                             :__FILE__
                                             :__LINE__
                                             :"Field=%s", __currentFieldName];
	}
}

/*
 * 从IOBuffer中反序列化出SceneMap各field的值。
 *
 * @param IOBuffer 储存数据流的buffer对象
 */
- (void) deseriallize:(IOBuffer *)ibuffer {
	char *__currentFieldName = "N/A";
	@try {
		// 反序列化域 id
		__currentFieldName = "id";
		self->ID = [MessageBuffer readInteger:ibuffer];

		// 反序列化域 data
		__currentFieldName = "data";
		self->data = [MessageBuffer readByteArray:ibuffer];

		// 反序列化域 prop
		__currentFieldName = "prop";
		self->prop = (Prop *) [MessageBuffer unpackBean:ibuffer :[Prop class]];

	} @catch (NSException *__ex) {
		@throw [MessageBuffer createException:@"AllType#deseriallize"
                                             :__ex
                                             :__FILE__
                                             :__LINE__
                                             :"Field=%s", __currentFieldName];
	}
}

@end

///////////////////////////////////////////////////////////////////////////////
// 消息CommandResponse
//
@implementation CommandResponse

@synthesize errorCode;
@synthesize errorMessage;

- (void) setErrorMessage:(NSString *)str {
	if (self->errorMessage != nil) {
		[self->errorMessage release];
		self->errorMessage = nil;
	}
	
	if (str != nil) {
		self->errorMessage = [[NSString alloc] initWithString:str];
	}
}

- (void) setErrorMessageWithCString:(const char *)str {
	if (self->errorMessage != nil) {
		[self->errorMessage release];
		self->errorMessage = nil;
	}
	
	if (str != nil) {
		self->errorMessage = [[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding];
	}
}

- (NSUInteger) getErrorMessageToCString:(char *)str :(int) maxLen {
	if (self->errorMessage != nil) {
    	[self->errorMessage getCString:str maxLength:maxLen encoding:NSUTF8StringEncoding];
		return [self->errorMessage length];
	} else {
		return 0;
	}
}


- (void) clear {
	[super clear];
	self->errorCode = 0;
	self->errorMessage = nil;
}

/*
 * 把各field的值设为0或者nil
 */
- (CommandResponse *) init {
	self = [super init];
	[self clear];
	return self;
}

/*
 * 释放消息CommandResponse占用的内存
 */
- (void) dealloc {
#ifdef DEBUG
	NSLog(@"**** MessageBean CommandResponse dealloc");
#endif

	if (self->errorMessage != nil) {
		[self->errorMessage release];
		self->errorMessage = nil;
	}
	
	[super dealloc];
}

/*
 * 把消息CommandResponse的信息完整地格式化输出到字符串中。
 *
 * @param string 用于格式化输出结果的字符串对象。
 * @param indentLevel 输出时的缩进级别
 */
- (void) printToStringWithIndent:(NSMutableString *)string :(int)indentLevel {
	[string appendFormat:@"CommandResponse [retainCount=%u] {\n", [self retainCount]];

	// 输出域 errorCode
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"int errorCode = "];
	[string appendFormat:@"%d\n", self->errorCode];

	// 输出域 errorMessage
	[self appendIndent:string :indentLevel + 1];
	[string appendString:@"String errorMessage = "];
	if (self->errorMessage == nil) {
		[string appendString:@"nil\n"];
	} else {
		[string appendFormat:@"[retainCount=%u] \"%@\"\n", [self->errorMessage retainCount], self->errorMessage];
	}


	[self appendIndent:string :indentLevel];
	[string appendString:@"}\n\n"];
}

/*
 * 把消息CommandResponse的各域序列化到IOBuffer中。
 *
 * @param obuffer 用于存放序列化结果字节流的buffer对象
 */
- (void) seriallize:(IOBuffer *)obuffer {
	char *__currentFieldName = "N/A";
	@try {
		// 序列化域 errorCode
		__currentFieldName = "errorCode";
		[MessageBuffer writeInteger:obuffer :self->errorCode];

		// 序列化域 errorMessage
		__currentFieldName = "errorMessage";
		[MessageBuffer writeString:obuffer :self->errorMessage];

	} @catch (NSException *__ex) {
		@throw [MessageBuffer createException:@"AllType#seriallize"
                                             :__ex
                                             :__FILE__
                                             :__LINE__
                                             :"Field=%s", __currentFieldName];
	}
}

/*
 * 从IOBuffer中反序列化出CommandResponse各field的值。
 *
 * @param IOBuffer 储存数据流的buffer对象
 */
- (void) deseriallize:(IOBuffer *)ibuffer {
	char *__currentFieldName = "N/A";
	@try {
		// 反序列化域 errorCode
		__currentFieldName = "errorCode";
		self->errorCode = [MessageBuffer readInteger:ibuffer];

		// 反序列化域 errorMessage
		__currentFieldName = "errorMessage";
		self->errorMessage = [MessageBuffer readString:ibuffer];

	} @catch (NSException *__ex) {
		@throw [MessageBuffer createException:@"AllType#deseriallize"
                                             :__ex
                                             :__FILE__
                                             :__LINE__
                                             :"Field=%s", __currentFieldName];
	}
}

@end

